﻿/// <reference path="MyAjax.js" />
/// <reference path="MyWebsite.js" />

var MyDatetime = {
    getServerNowTime: function (sType) {
        /// <summary>获取服务器当前的时间</summary>
        var sUrl = MyWebsite.getServerAddr() + "Common/Agents/GetServerNowTime.ashx";
        return MyAjax.get(sUrl, { t: sType }, false);
    },
    format: function (timeStr, inputType, targetType) {
        /// <summary>根据日期字符串创建一个日期对象</summary>
        /// <param name="timeStr" type="String">输入的日期字符串</param>
        /// <param name="inputType" type="String">输入的日期字符串的格式，例如：yyyy-MM-dd HH:mm:ss；如果不指定此参数，则默认当做：yyyy-MM-dd的方式处理</param>
        /// <param name="targetType" type="String">期待输出的日期格式，例如：yyyy-MM-dd HH:mm:ss；如果不指定此参数，则默认当做：yyyy-MM-dd的方式处理</param>
        /// <returns type="String">返回格式化好的日期</return>
        if (!inputType) inputType = "yyyy-MM-dd";
        if (!targetType) targetType = "yyyy-MM-dd";
        var oDate = this.createDateObj(timeStr, inputType);
        //还需要替换不存在的日期情况
        targetType = targetType.replace("yyyy", oDate.getFullYear());
        targetType = targetType.replace("MM", oDate.getMonth());
        targetType = targetType.replace("dd", oDate.getDate());
        targetType = targetType.replace("HH", oDate.getHours());
        targetType = targetType.replace("mm", oDate.getMinutes());
        targetType = targetType.replace("ss", oDate.getSeconds());
        return targetType;
    },
    createDateObj: function (timeStr, inputType) {
        /// <summary>根据日期字符串创建一个日期对象</summary>
        /// <param name="timeStr" type="String">输入的日期字符串</param>
        /// <param name="inputType" type="String">输入的日期字符串的格式，例如：yyyy-MM-dd HH:mm:ss；如果不指定此参数，则默认当做：yyyy-MM-dd的方式处理</param>
        /// <returns type="DateObject">返回javascript日期对象</return>
        if (!inputType) inputType = "yyyy-MM-dd";
        var dateTimeStr = "yyyy/MM/dd HH:mm:ss";
        if (timeStr) {
            var charStr = '',
            c, //枚举过程中的每一个字符
            iStart = 0, //记录在枚举时的开始位置
            sValidateChar = "yMdHms"; //枚举合法的字母
            for (var i = 0; i < inputType.length; i++) {
                c = inputType.charAt(i);
                if (sValidateChar.indexOf(c) == -1) {
                    //还要重置标志参数
                    charStr = "";
                    //标志下一个字符是开始的字符
                    iStart = i + 1;
                    continue;
                }
                charStr += c;
                switch (charStr) {
                    case 'yyyy':
                        var sTemp = timeStr.substr(iStart, i - iStart + 1);
                        if (sTemp) {
                            dateTimeStr = dateTimeStr.replace("yyyy", sTemp);
                            iStart = i + 1; //充值开始的标志
                            charStr = "";
                        }
                        break;
                    case 'MM':
                        var sTemp = timeStr.substr(iStart, i - iStart + 1);
                        if (sTemp) {
                            dateTimeStr = dateTimeStr.replace("MM", timeStr.substr(iStart, i - iStart + 1));
                            iStart = i + 1; //充值开始的标志
                            charStr = "";
                        }
                        break;
                    case 'dd':
                        var sTemp = timeStr.substr(iStart, i - iStart + 1);
                        if (sTemp) {
                            dateTimeStr = dateTimeStr.replace("dd", timeStr.substr(iStart, i - iStart + 1));
                            iStart = i + 1; //充值开始的标志
                            charStr = "";
                        }
                        break;
                    case 'HH':
                        var sTemp = timeStr.substr(iStart, i - iStart + 1);
                        if (sTemp) {
                            dateTimeStr = dateTimeStr.replace("HH", timeStr.substr(iStart, i - iStart + 1));
                            iStart = i + 1; //充值开始的标志
                            charStr = "";
                        }
                        break;
                    case 'mm':
                        var sTemp = timeStr.substr(iStart, i - iStart + 1);
                        if (sTemp) {
                            dateTimeStr = dateTimeStr.replace("mm", timeStr.substr(iStart, i - iStart + 1));
                            iStart = i + 1; //充值开始的标志
                            charStr = "";
                        }
                        break;
                    case 'ss':
                        var sTemp = timeStr.substr(iStart, i - iStart + 1);
                        if (sTemp) {
                            dateTimeStr = dateTimeStr.replace("ss", timeStr.substr(iStart, i - iStart + 1));
                            iStart = i + 1; //充值开始的标志
                            charStr = "";
                        }
                        break;
                }
            }
        }
        //还需要替换不存在的日期情况
        dateTimeStr = dateTimeStr.replace("yyyy", "0000");
        dateTimeStr = dateTimeStr.replace("MM", "00");
        dateTimeStr = dateTimeStr.replace("dd", "00");
        dateTimeStr = dateTimeStr.replace("HH", "00");
        dateTimeStr = dateTimeStr.replace("mm", "00");
        dateTimeStr = dateTimeStr.replace("ss", "00");
        return new Date(dateTimeStr);
    }
};